package com.smfx.yxt.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.SysResource;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.service.ISysResourceService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.view.SysResourceView;
import com.smfx.yxt.vo.SysResourceQueryVO;

@RequestMapping("module/manage")
@Controller
public class ModuleManageController {
	
	private Logger logger = LoggerFactory.getLogger(ModuleManageController.class);

	@Autowired
	private ISysResourceService sysResourceService;
	
	@RequestMapping(value = {"", "/"})
	public String sysModuleManage() {
		return "sys/sysModuleManage";
	}
	
	@RequestMapping("teacher")
	public String teacherModuleManage() {
		return "sys/teacherModuleManage";
	}
	
	@RequestMapping("parent")
	public String parentModuleManage() {
		return "sys/parentModuleManage";
	}
	
	@RequestMapping("query")
	@ResponseBody Object schoolQuery(SysResourceQueryVO vo) {
		try {
			Page<SysResource> page = new Page<SysResource>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("PARENT_ID"));
			sorts.add(new OrderBy("WEIGHT"));
			page.setSorts(sorts);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<SysResource> list = sysResourceService.selectByPage(page);
			List<SysResourceView> listView = BeanUtils.copyAs(list, SysResourceView.class);
			grid.setRows(listView);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---query error={}", e);
		}
		return null;
	}
	
	@RequestMapping("addAndUpdate")
	@ResponseBody Object addAndUpdate(SysResource record) {
		try {
			sysResourceService.addAndUpdateSysResource(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---addAndUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("batchDel")
	@ResponseBody Object batchDel(@RequestParam("ids[]")List<Integer> ids){
		try {
			sysResourceService.updateBatchDel(ids);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--------batchDel error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	
}
